<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
        <h1>已生成一个1-100的随机数</h1>
        <input type="text" id="guess">
        <input type="button" onclick="guessNum()" value="点我验证">
        <h1 id="res"></h1>
        <span></span>

        <script>
            let num = parseInt(Math.random() * 100) + 1;
            document.querySelector("span").innerHTML = num;
            let count = 0;
           function guessNum() {
                   let element = document.getElementById("res");
                   let guess = document.getElementById("guess").value;
                   if (num > guess) {
                       element.innerText = "猜小了";
                       count++;
                   } else if (num < guess) {
                       element.innerText = "猜大了";
                       count++;
                   } else {
                       element.innerText = "恭喜你,用了" + count + "就猜对了";
                   }
           }
        </script>
</body>
</html>